package com.googlecode.dtools.typeinfo.graphviz.map.impl;

import com.googlecode.dtools.typeinfo.beans.DmType;
import com.googlecode.dtools.typeinfo.beans.DmTypeAttr;
import com.googlecode.dtools.typeinfo.graphviz.bean.DmLink;
import com.googlecode.dtools.typeinfo.graphviz.map.Mapper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @author A_Reshetnikov
 * @since Date: 22.05.12 Time: 17:25
 */
public class MapperImpl implements Mapper {
    public List<DmLink> map2DmLinkList(DmType dmType) {
        List<DmLink> list = new ArrayList<DmLink>();
        for (DmTypeAttr dmTypeAttr : dmType.getAttributeList().getAttribute()) {
            List<DmLink> list2 = mapDmTypeAttr2DmLinkList(dmType.getName(), dmTypeAttr);
            if (list2 != null)
                list.addAll(list2);
        }
        return list;
    }

    private List<DmLink> mapDmTypeAttr2DmLinkList(String parentName, DmTypeAttr dmTypeAttr) {
        List<DmLink> list = new ArrayList<DmLink>();
        if (dmTypeAttr.getFkReferenceList() == null) return null;
        for (String child : dmTypeAttr.getFkReferenceList().getFkReference()) {
            DmLink dmLink = new DmLink();
            dmLink.setParent(parentName);
            dmLink.setChild(child);
            dmLink.setName(dmTypeAttr.getName());
            list.add(dmLink);
        }
        return list;
    }
}
